//
//  LYMusicViewController.m
//  MusicMV
//
//  Created by Eternity on 16/3/17.
//  Copyright © 2016年 Eternity. All rights reserved.
//

#import "LYMusicViewController.h"


@interface LYMusicViewController ()<UITableViewDataSource, UITableViewDelegate, LYMusicTableViewCellDelegate>

{
    //HUD（Head-Up Display，意思是抬头显示的意思）
    MBProgressHUD *HUD;
}
@property (nonatomic, retain) UITableView *musicTableView;/**< 页面的tableView */

@property (nonatomic, retain) NSMutableArray *sourceData; /**< 源数据的数组 */




@end

@implementation LYMusicViewController


- (void)dealloc {
    
    _musicTableView.delegate = nil;
    _musicTableView.dataSource = nil;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"音乐";
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"电台" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    self.sourceData = [NSMutableArray array];
    // 创建tableView
    [self createView];
    
    // 数据解析
    [self dataHandle];
    
    // 添加了加载的动画
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.labelText = @"正在加载";
    
    //设置模式为进度框形的
    HUD.mode = MBProgressHUDModeDeterminate;
    [HUD showAnimated:YES whileExecutingBlock:^{
        float progress = 0.0f;
        while (progress < 1.0f) {
            progress += 0.01f;
            HUD.progress = progress;
            usleep(50000);
        }
    } completionBlock:^{
        [HUD removeFromSuperview];
        HUD = nil;
    }];
    
    
    
}

- (void)itemAction:(UIBarButtonItem *)item {
    
    LYRadioViewController *RadioVc = [[LYRadioViewController alloc] init];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:RadioVc];
    nav.navigationBar.backgroundColor = [UIColor blackColor];
    
    [LY_sharePlayeMusicHelp pause];
    
    nav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    
   [self presentViewController:nav animated:YES completion:^{
       
   }];
}

// 创建页面
- (void)createView {
    self.musicTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT  - 64) style:UITableViewStylePlain];
    [self.view addSubview:_musicTableView];
    _musicTableView.delegate = self;
    _musicTableView.dataSource = self;
    
    _musicTableView.rowHeight = 400 * ht;
    _musicTableView.showsVerticalScrollIndicator = NO;
    
    _musicTableView.showsVerticalScrollIndicator = NO;
    _musicTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _musicTableView.backgroundColor = [UIColor blackColor];
    [_musicTableView registerClass:[LYMusicTableViewCell class] forCellReuseIdentifier:@"MusicTableView"];
    
}

#pragma mark - tableView delegate & datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _sourceData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LYMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MusicTableView"];
    
    LYMusicModel *model = _sourceData[indexPath.section];
    cell.model = model;
    cell.indexPath = indexPath;
    cell.delegate = self;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }else {
        return 15 * ht;
    }
}


#pragma mark - 协议方法
- (void)jumpNextPage:(NSIndexPath *)indexPath {
    
    
        LYMusicPlayerViewController *musicVc = [[LYMusicPlayerViewController alloc] init];
        musicVc.musicArr = _sourceData;
    
        [musicVc setMusicArr:_sourceData index:indexPath.section];

        self.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    
        [self presentViewController:musicVc animated:YES completion:^{
            
        }];
}

#pragma mark - 数据解析
- (void)dataHandle {
    
    [XRAFNetworkTool POSTUrl:@"http://api.enjoymusicapp.com/api/v2.0/feed/hot.ema?sign=c4aaa213b11421bbbbda832843572516&uid=0&version=2.3.2&platform=ios" body:@{@"next_key":@"0",@"stype":@"1",@"uid":@"0"} requestBodyStyle:XRRequestJSON headFile:@{@"Content-Type":@"application/json",@"charset":@"UTF-8"} response:XRJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
//        NSLog(@"%@", responseObject);
        
        
        NSArray *tempArr = responseObject[@"data"][@"items"];
        self.sourceData = [LYMusicModel modelWithArray:tempArr];
        
        [_musicTableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
